package com.project.sianet.data;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.project.sianet.R;
import com.project.sianet.entidades.*;
import com.project.sianet.util.TypefaceManager;

public class LibretaArrayAdapter extends ArrayAdapter<Libreta> {

	private static final String tag = "LibretaArrayAdapter";
    private TextView area, nota;
    private ImageView icono;
    private List<Libreta> libretas = new ArrayList<Libreta>();
    private int mBimestre, mCantBim;
    private int[] alternateColors = new int[] { 0xFFFFFFFF, Color.rgb(240,240,240) };
    
    public LibretaArrayAdapter(Context context, int textViewResourceId,
            List<Libreta> objects, int bimestre, int cantBim) {
        super(context, textViewResourceId, objects);
        this.libretas = objects;
        this.mBimestre = bimestre;
        this.mCantBim = cantBim;
    }
    
    public int getCount() {
        return this.libretas.size();
    }
 
    public Libreta getItem(int index) {
        return this.libretas.get(index);
    }
    
    @Override
    public boolean isEnabled(int position) {
    	
    	Libreta lib = getItem(position);
    	
    	return !lib.getEsArea();

    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            // ROW INFLATION
            Log.d(tag, "Starting XML Row Inflation ... ");
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.libreta_areas_list, parent, false);
            Log.d(tag, "Successfully completed XML Row Inflation!");
        }
        
        int colorPos = position % alternateColors.length;
        row.setBackgroundColor(alternateColors[colorPos]);
 
        Libreta lib = getItem(position);
        
        area = (TextView) row.findViewById(R.id.libreta_areas_nombre);
        nota = (TextView) row.findViewById(R.id.libreta_areas_nota);
        icono = (ImageView) row.findViewById(R.id.libreta_areas_ico);
 
        if (TextUtils.isEmpty(lib.getCurso())) {
        	row.setBackgroundColor(Color.rgb(220,234,244));
        	area.setText(lib.getArea());
        	area.setPadding(0, 0, 0, 0);
        	area.setTypeface(TypefaceManager.getInstance().getTypeface("MavenPro"), Typeface.BOLD);
        	nota.setTypeface(TypefaceManager.getInstance().getTypeface("MavenPro"), Typeface.BOLD);
        	nota.setTextSize(16);
        } else {
        	area.setText(lib.getCurso());
        	area.setPadding(50, 0, 0, 0);
        	area.setTypeface(TypefaceManager.getInstance().getTypeface("MavenPro"), Typeface.NORMAL);
        	nota.setTypeface(TypefaceManager.getInstance().getTypeface("MavenPro"), Typeface.NORMAL);
        	nota.setTextSize(14);
        }
        
        switch(mBimestre) {
        	case 1:
        		nota.setText(Html.fromHtml(lib.getNota1()));
        		break;
        	case 2:
        		if (mCantBim == 1)
        			nota.setText(Html.fromHtml(lib.getNotaFinal()));
        		else
        			nota.setText(Html.fromHtml(lib.getNota2()));
        		break;
        	case 3:
        		if (mCantBim == 2)
        			nota.setText(Html.fromHtml(lib.getNotaFinal()));
        		else
        			nota.setText(Html.fromHtml(lib.getNota3()));
        		break;
        	case 4:
        		if (mCantBim == 3)
        			nota.setText(Html.fromHtml(lib.getNotaFinal()));
        		else
        			nota.setText(Html.fromHtml(lib.getNota4()));
        		break;
        	default:
        		nota.setText(Html.fromHtml(lib.getNotaFinal()));
        		break;
        }
        
        if (nota.getText().toString().contains("*")) {
        	nota.setVisibility(View.GONE);
        	icono.setVisibility(View.VISIBLE);
        } else {
        	nota.setVisibility(View.VISIBLE);
        	icono.setVisibility(View.GONE);
        }

        return row;
    }
	
}
